package jmu.cze.controller;

import jmu.cze.service.NoteService;
import jmu.cze.until.JsonUtil;
import jmu.cze.vo.Message;
import jmu.cze.vo.Revert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
@RequestMapping("/note")
public class NoteController {
    private NoteService noteService;

    @Resource(name = "noteServiceImpl")
    public void setNoteService(NoteService noteService) {
        this.noteService = noteService;
    }


    @GetMapping("/getMessage")
    public String getMessage(@RequestParam("productID") int productID,@RequestParam("page") int page){
        return JsonUtil.getJson(1,"list", noteService.getMessageByPage(productID, page, 5));
    }

    @GetMapping("/getRevert")
    public String getRevert(int messageID){
        return JsonUtil.getJson(1,"list", noteService.getRevertByMessageID(messageID));
    }

    @PostMapping("/deleteMessage")
    public String deleteMessage(int messageID, HttpSession session){
        if(noteService.deleteMessage(messageID, (String) session.getAttribute("id"))){
            return  JsonUtil.success("删除成功");
        }
        return  JsonUtil.failure("删除失败");
    }

    @PostMapping("/deleteRevert")
    public String deleteRevert(int revertID, HttpSession session){
        if(noteService.deleteRevert(revertID, (String) session.getAttribute("id"))){
            return  JsonUtil.success("删除成功");
        }
        return  JsonUtil.failure("删除失败");
    }

    @PostMapping("/addMessage")
    public String addMessage(Message message, HttpSession session){
        message.setWriterID((String) session.getAttribute("id"));
        message.setWriteDate(new Date());
        if (noteService.addMessage(message)) {
            return  JsonUtil.success("添加成功");
        }
        return  JsonUtil.failure("添加失败");
    }

    @PostMapping("/addRevert")
    public String addRevert(Revert revert, HttpSession session){
        revert.setWriterID((String) session.getAttribute("id"));
        revert.setWriteDate(new Date());
        if (noteService.addRevert(revert)) {
            return  JsonUtil.success("添加成功");
        }
        return  JsonUtil.failure("添加失败");
    }

    @PostMapping("/updateMessage")
    public String updateMessage(Message message){
        message.setWriteDate(new Date());
        if (noteService.updateMessage(message)){
            return  JsonUtil.success("修改成功");
        }
        return  JsonUtil.failure("修改失败");
    }

    @PostMapping("/updateRevert")
    public String updateRevert(Revert revert){
        revert.setWriteDate(new Date());
        if (noteService.updateRevert(revert)) {
            return  JsonUtil.success("修改成功");
        }
        return  JsonUtil.failure("修改失败");
    }
}
